import {
  food_detail,
  food_like,
  food_collect,
  food_comment,
  food_comment_list
} from '../../network/interface'
let msg;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Sid: 0,
    showShare: false,
    isLike: false,
    actionSheetHidden: true,
    commentList:[],
    options: [
      [{
          name: '微信',
          icon: 'wechat',
          openType: 'share'
        },
        {
          name: '朋友圈',
          icon: 'wechat-moments',
          openType: 'share'
        },
        {
          name: '微博',
          icon: 'weibo',
          openType: 'share'
        },
        {
          name: 'QQ',
          icon: 'qq',
          openType: 'share'
        },
        {
          name: '分享海报',
          icon: 'poster',
          openType: 'share'
        },
      ]
    ],
    foodDetail: {}
  },
  onClick() {
    this.setData({
      showShare: true
    });
  },
  onClose() {
    this.setData({
      showShare: false
    });
  },

  //将输入的内容绑定到 msg 中
  obtainInput: function (data) {
    msg = data.detail.value;
  },

  listenerButton: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    });
  },

  listenerActionSheet: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },

  //用户输完并点击确认后，输入的信息会打印到控制台上
  showInput(e) {
    console.log(e)
    let params={}
    var that=this;
    params.comment=msg
    params.foodId=e.currentTarget.dataset.id
    food_comment(params,res=>{
      console.log(res)
      wx.showToast({
        title: '评论成功',
        icon:'none'
      })
      that.getFoodComments(e.currentTarget.dataset.id)
    })
  },

 


  likeFood(e) {
    console.log(e.currentTarget.dataset.id)
    console.log(e.currentTarget.dataset.stauts)
    var foodId = e.currentTarget.dataset.id
    var status = e.currentTarget.dataset.status

    if (status) {
      let params = {}
      params.foodId = foodId;
      params.type = 2
      var that = this
      food_like(params, res => {
        if (res['status'] === 200) {
          that.refreshFoodDetail(foodId)
        }
      })
    } else {
      let params = {}
      params.foodId = foodId;
      params.type = 1
      var that = this
      food_like(params, res => {
        if (res['status'] === 200) {
          that.refreshFoodDetail(foodId)
          wx.showToast({
            title: '点赞成功',
            icon: 'none'
          })
        }
      })
    }



  },


  collectFood(e) {
    console.log(e.currentTarget.dataset.id)
    console.log(e.currentTarget.dataset.stauts)
    var foodId = e.currentTarget.dataset.id
    var status = e.currentTarget.dataset.status

    if (status) {
      let params = {}
      params.foodId = foodId;
      params.type = 2
      var that = this
      food_collect(params, res => {
        if (res['status'] === 200) {
          that.refreshFoodDetail(foodId)
        }
      })
    } else {
      let params = {}
      params.foodId = foodId;
      params.type = 1
      var that = this
      food_collect(params, res => {
        if (res['status'] === 200) {
          that.refreshFoodDetail(foodId)
          wx.showToast({
            title: '收藏成功',
            icon: 'none'
          })
        }
      })
    }
  },

  onSelect(event) {
    Toast(event.detail.name);
    this.onClose();
  },
  foodlistType: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      Sid: index,
    });
  },

  toshouye: function () {
    wx.switchTab({
      url: '/pages/foodMain/foodMain',
    })
  },
  tocomment: function () {
    wx.navigateTo({
      url: '/pages/comment/comment',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFoodDetail(options.id)
    this.getFoodComments(options.id)
  },

  getFoodComments(id){
    let params={}
    params.foodId=id;
  food_comment_list(params,res=>{
    console.log(res)
    this.setData({
      commentList:res.data
    })
  })

  },

  refreshFoodDetail(id) {
    let params = {}
    params.id = id;
    var that = this;
    food_detail(params, res => {
      that.setData({
        foodDetail: res.data
      })
      wx.hideLoading({
        success: (res) => {},
      })
    })
  },

  getFoodDetail(id) {
    let params = {}
    params.id = id;
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    food_detail(params, res => {
      that.setData({
        foodDetail: res.data
      })
      wx.hideLoading({
        success: (res) => {},
      })
    })

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var index = 0
    index = wx.getStorageSync('Sid')
    this.setData({
      Sid: index,
    });
  },


})